//
//  NewStrategyListViewController.m
//  WisdomZhuHai
//
//  Created by BABLE on 15/5/4.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "NewStrategyListViewController.h"
#import "ProtocolDefine.h"
#import "TransforController.h"
#import "Common.h"
#import "UIWebControl.h"
#import "StrategyDetailViewController.h"
#import "StrategySubViewController.h"

const float TOPIMAGECELL_HEIGHT = 143;//列表高

@interface NewStrategyListViewController ()
@property (nonatomic,strong)NSArray *newsList;
@property (nonatomic,strong)NSDictionary *ApiDic;

@property (nonatomic,strong)WebIOS *web;
@end

@implementation NewStrategyListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"玩法";
    [self getClassify];
}
- (void)getClassify
{
    [[ProtocolManager shareInstance] getClassify:self selector:@selector(didFinishGetNewStrategyList:)];
}
-(void)didFinishGetNewStrategyList:(NSDictionary *)dic{
    if(dic == nil)
    {
        NSLog(@"didFinishGetNewStrategyList dic is nil");
        return ;
    }
    self.ApiDic = dic;
    
    id list = [Common valueForKey:P_LIST dic:dic];
    if (![list isKindOfClass:[NSArray class]]) {
        return;
    }
    
    self.newsList = list;
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-100) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [Common colorWithRGB:0xdbdcdc];
    
    [TransforController shardeInstanceOfNavigationBarCoverViewWithY:0 andTitle:@""];
    
    [self.view addSubview:tableView];
    [[Common shareInstance]stopLoadingView];
}


//构建CELL
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    
    if (indexPath.row >= self.newsList.count) {
        static NSString *pageCellIdentifier = @"nilCell";
        cell = [tableView dequeueReusableCellWithIdentifier:pageCellIdentifier];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pageCellIdentifier];
        }
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        [cell addSubview:view];
        return cell;
    }
    
    static NSString *pageCellIdentifier = @"CommonBigImageCell";
    cell = [tableView dequeueReusableCellWithIdentifier:pageCellIdentifier];

    if(cell == nil)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:pageCellIdentifier  owner:self options:nil][0];
    }

    UIImageView *imgView = (UIImageView*)[cell viewWithTag:100001];
    NSDictionary* dic = self.newsList[indexPath.row];
    NSString *classifyId = [[Common valueForKey:P_ID dic:dic] intValue];
    
    NSString * imgUrl = [Common valueForKey:P_ALBUM2 dic:dic];//获取图片
    NSLog(@"*******图片地址为：%@ ",imgUrl);
    if(imgUrl != nil){
        [imgView setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
    }

    UILabel *strategyName = (UILabel *)[cell viewWithTag:100002];
    NSString * name = [Common valueForKey:P_NAME dic:dic];//获取攻略名
    if(name != nil){
        strategyName.text = name;
    }
    
    return cell;
    
}

//单击CELL动作
//-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *sId = [Common valueForKey:P_ID dic:self.newsList[indexPath.row]];

//    [self pushStrategySubViewWithId:sId];
    StrategySubViewController *strategySubViewCtrl = [[StrategySubViewController alloc] initWithFrame:[Common shareInstance].contentFrameNoTabBar];
    strategySubViewCtrl.classifyId = [sId intValue];
    strategySubViewCtrl.hidesBottomBarWhenPushed = YES;
    strategySubViewCtrl.navigationCtrl = self.navigationCtrl;
    [self.navigationCtrl pushViewController:strategySubViewCtrl animated:YES];
}
//-(void)didFinshPushStrategyDetails:(NSDictionary *)dic{
//    NSLog(@"getContentCallback %@", dic);
//    if (dic == nil) {
//        return;
//    }
//    StrategySubViewController *strategySubViewCtrl = [[StrategySubViewController alloc] initWithFrame:[Common shareInstance].contentFrameNoTabBar];
//    strategySubViewCtrl.classifyId = gesture.view.tag;
//    strategySubViewCtrl.hidesBottomBarWhenPushed = YES;
//    strategySubViewCtrl.navigationCtrl = self.navigationCtrl;
//    [self.navigationCtrl pushViewController:strategySubViewCtrl animated:YES];
//}


//表格属性
//CELL数目
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.newsList.count;
}
//CELL尺寸
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return TOPIMAGECELL_HEIGHT;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
